// Copyright 2007 Novint Technologies, Inc. All rights reserved.
// Available only under license from Novint Technologies, Inc.
// Make sure this header is included only once
#ifndef HAPTICS_H
#define HAPTICS_H
#include <hdl/hdl.h>
#include <hdlu/hdlu.h>

// Blocking values
const bool bNonBlocking = false;
const bool bBlocking = true;

class HapticsClass 
{
// Define callback functions as friends
friend HDLServoOpExitCode ContactCB(void *data);
friend HDLServoOpExitCode GetStateCB(void *data);

public:
    HapticsClass();						// Constructor
    ~HapticsClass();					// Destructor
    void init(double a_stiffness);		// Initialize
    void uninit();					    // Clean up
    void getPosition(double pos[3]);	// Get position
    void setPosition(double pos[3]);	// Get position

    bool isButtonDown();				// Get state of device button
    bool m_inited;						// Nothing happens until initialization is done
    double m_Stiffness;

private:
    void synch();						// Move data between servo and app variables
    void vecMultMatrix(double srcVec[3], double mat[16], double dstVec[3]);    // Matrix multiply
    void testHDLError(const char* str);	// Check error result; display message and abort, if any
    // synchFromServo() is called from the application thread when it wants to exchange
    // data with the HapticClass object.  HDAL manages the thread synchronization
    // on behalf of the application.
    void synchFromServo();
	void FFB ();


    HDLDeviceHandle m_deviceHandle;		// Handle to device
    HDLServoOpExitCode m_servoOp;		// Handle to Contact Callback 
    bool isDeviceCalibrated();			// Get ready state of device.
    double m_transformMat[16];			// Transformation from Device coordinates to Application coordinates
    double m_positionServo[3];			// Variables used only by servo thread
    bool   m_buttonServo;
    double m_positionApp[3];			// Variables used only by application thread
    double m_positionExt[3];			// Variables used only by application thread
    bool   m_buttonApp;
    int    m_lastFace;					// Keep track of last face to have contact
    double m_workspaceDims[6];			// Device workspace dimensions
    double m_positionPre[3];
    double m_forceServo[3];

};

#endif // HAPTICS_H